<?php
//首页调用
namespace Store\Controller;
use Think\Controller;
class MemberController extends CommonController {
    //会员列表
    public function index(){
        $where=array();
        $where['yt_member.store_id']=session('store_id');
        if($_GET['phone']){
            $where['yt_users.phone']=$_GET['phone'];
        }
        $memberModel   =   D('member');
        $member_grade   =   D('member_grade');
        $count      = $memberModel->field('yt_users.username,yt_users.phone,yt_member.*')
            ->join('yt_users on yt_users.id=yt_member.user_id')
            ->where($where)->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show       = $Page->show();// 分页显示输出
        $member_list=$memberModel
            ->field('yt_users.username,yt_users.phone,yt_member.*')
            ->join('yt_users on yt_users.id=yt_member.user_id')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        $grade_list=$member_grade->where('store_id='.session('store_id'))->select();
        if(is_array($member_list)){
             foreach ($member_list as $key=>$val){
                 //print_r($val['grade']);
                 $grade= $member_grade->find($val['grade']);
                 //print_r($grade);
                 $member_list[$key]['gradeName']=$grade['gradeName'];
             }
        }
        //print_r($grade_list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('member_list',$member_list);
        $this->assign('grade_list',$grade_list);
        $this->assign('title','会员列表');
        $this->display();
    }
     //会员等级
    public function grade(){
        $where=array();
        $where['store_id']=session('store_id');
        /*if($_GET['phone']){
            $where['phone']=session('store_id');
        }*/
        $member_grade   =   D('member_grade');
        $count      = $member_grade->where($where)->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show       = $Page->show();// 分页显示输出
        $grade_list=$member_grade
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        //print_r($member_grade->getLastSql());
        //print_r($grade_list);

        $this->assign('page',$show);// 赋值分页输出
        $this->assign('grade_list',$grade_list);
        $this->assign('title','会员等级列表');
        $this->display();
    }
    //会员卡
    /*public function member_card(){
        $where=array();
        $where['store_id']=session('store_id');
        if($_GET['phone']){
            $where['phone']=session('store_id');
        }
        $member_card   =   D('member_card');
        $count      = $member_card->where($where)->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show       = $Page->show();// 分页显示输出
        $card_list=$member_card
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('member_list',$card_list);
        $this->assign('title','会员列表');
        $this->display();
    }*/
   //消费统计
    public function expense()
    {
        $storeArray = $this->storeArray;
        $store_id = $storeArray['id'];
        $count_rs = M('order')
            ->join('yt_member on yt_member.id=yt_order.member_id')
            ->where("yt_order.store_id=$store_id and yt_order.order_state>10 and yt_order.refund_state=0")
            ->group('yt_order.member_id')
            ->select();
        $count = count($count_rs);
        $Page = new \Think\Page ($count, 15); // 实例化分页类 传入总记录数
        $Page->setConfig('header', '条记录');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $show = $Page->show(); // 分页显示输出
        $rs = M('order')
            ->join('yt_member on yt_member.id=yt_order.member_id')
            ->where("yt_order.store_id=$store_id and yt_order.order_state>10 and yt_order.refund_state=0")
            ->group('yt_order.member_id')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('page', $show);
        $this->assign('rs', $rs);
        $this->display();
    }
    public function addmember(){
       $this->display();
    }
    public function addgrade(){
        $this->display();
    }

    function member_add(){
        $storeArray = $this->storeArray;
        $store_id = $storeArray['id'];
        $data=I('post.');
        $users=M('users');
        if($data['type']==1){
            $uid=$data['uid'];
            $rand=rand(0,$store_id.$uid);
            $card_no = getCard($rand,6);
            //print_r('card_no: '.$card_no.'    ');
            $card_pre = $store_id;
            $card_vc = substr(md5($card_pre.$card_no),0,2);
            $card_vc = strtoupper($card_vc);
            $memberId=$card_pre.$card_no.$card_vc;
            $member['user_id']=$uid;
            $member['memberId']=$memberId;
            $member['store_id']=$store_id;
            $member['grade']=$data['grade'];
            $member['integral']=$data['integral'];
            $member['money']=$data['money'];
            $member['start_time']=time();
            $member['end_time']= strtotime($data['end_time']);
            $member['create_time']=time();
            $member['status']=1;
            //print_r($member);
            $member_id=M('member')->add($member);
            if($member_id){
                $this->ajax_r(1,"添加会员成功",array('url' => U('Member/index')));
            }else{
                $this->ajax_r(0,"添加会员失败",array('url' => U('Member/addmember')));
            }
        }else{
            $info['username']=$data['nickname']?$data['nickname']:$data['mobile'];
            $info['password']=passport_encrypt($data['password'],'wnsc');
            $info['ishidden']=$data['status'];
            $info['phone']=$data['mobile'];
            $info['addtime']=time();
            $info['sex']=$data['sex'];
            $rs=$users->where('phone='.$data['mobile'])->find();
            if($rs){
                $this->ajax_r(2,"该账号已存在");
            }else {
                $rs = $users->add($info);
                print_r($users->getLastSql());
                if ($rs) {
                    $rand = rand(0, $store_id . $rs);
                    $card_no = getCard($rand, 6);
                    print_r('card_no: ' . $card_no . '    ');
                    $card_pre = $store_id;
                    $card_vc = substr(md5($card_pre . $card_no), 0, 2);
                    $card_vc = strtoupper($card_vc);
                    $memberId = $card_pre . $card_no . $card_vc;
                    $member['user_id'] = $rs;
                    $member['memberId'] = $memberId;
                    $member['store_id'] = $store_id;
                    $member['grade'] = $data['grade'];
                    $member['integral'] = $data['integral'];
                    $member['money'] = $data['money'];
                    $member['start_time'] = time();
                    $member['end_time'] = strtotime($data['end_time']);
                    $member['create_time'] = time();
                    $member['status'] = 1;
                    print_r($member);
                    $member_id = M('member')->add($member);
                    if ($member_id) {
                        $this->ajax_r(1, "添加会员成功", array('url' => U('Member/index')));
                    } else {
                        $this->ajax_r(0, "添加会员失败", array('url' => U('Member/addmember')));
                    }
                } else {
                    $this->ajax_r(0, "添加用户失败", array('url' => U('Member/addmember')));
                }
            }
        }
    }


    //充值
    function recharge(){
        $data=I('post.');
        $memberModel=M('member');
        $users_log=M('users_log');
        $info['id']=$data['id'];
        $rs=$memberModel->find($data['id']);
        if(empty($rs)){
            $this->ajax_r(0,"该会员不存在");
        }
        $info['money']=$rs['money']+$data['num'];
        $rs=$memberModel->save($info);

        $log['uid']=$rs['user_id'];
        $log['member_id']=$rs['id'];
        $log['addtime']=time();
        $log['money']=abs($data['num']);
        if($data['num']<0){
            $log['type']=0;
        }else{
            $log['type']=1;
        }
        $log['other']='充值';
        $log['money_type']=4;
        $users_log->add($log);
        if($rs){
            $this->ajax_r(1,"充值成功",array('url' => U('Member/index')));
        }else{
            $this->ajax_r(0,"充值失败");
        }
    }
    //积分修改
    function integral_modify(){
        $data=I('post.');
        $storeArray = $this->storeArray;
        $store_id = $storeArray['id'];
        $memberModel=M('member');
        $info['id']=$data['id'];
        $rs1=$memberModel->find($data['id']);
        if(empty($rs1)){
            $this->ajax_r(0,"该会员不存在");
        }
        $info['integral']=$rs1['integral']+$data['num'];
        $rs=$memberModel->save($info);

        if($rs){
            $log['uid']=$rs1['user_id'];
            $log['member_id']=$rs1['id'];
            $log['addtime']=time();
            $log['num']=abs($data['num']);
            $log['pre_num']=abs($rs1['integral']);
            if($data['num']<0){
                $log['type']=0;
            }else{
                $log['type']=1;
            }
            $log['store_id']=$store_id;
            $log['remark']='店铺后台积分修改';
            $log['num_type']=1;
            M('member_log')->add($log);
            $this->ajax_r(1,"积分修改成功",array('url' => U('Member/index')));
        }else{
            $this->ajax_r(0,"积分修改失败");
        }
    }


    //添加店铺会员等级

    function grade_add(){
        $data=I('post.');
        $storeArray = $this->storeArray;
        $store_id = $storeArray['id'];
        $member_grade=M('member_grade');
        $info['gradeName']=$data['gradeName'];
        $info['money']=$data['money'];
        $info['min_integral']=$data['min_integral'];
        $info['discount']=$data['discount'];
        $info['points']=$data['points'];
        $info['daiyan']=$data['daiyan'];
        $info['store_id']=$store_id;
        $info['status']=1;
        $info['create_time']=time();
        //print_r($info);
        $rs=$member_grade->add($info);
        //print_r($member_grade->getLastSql());
        if($rs){
            $this->ajax_r(1,"添加等级成功",array('url' => U('Member/grade')));
        }else{
            $this->ajax_r(0,"添加等级失败",array('url' => U('Member/addgrade')));
        }
    }

    public function getgradedetail(){
        $data=I('post.');
        $member_grade=M('member_grade');
        $id=$data['id'];
        $rs=$member_grade->find($id);
        $this->ajax_r(1,"成功",$rs);
    }
    //会员等级变更
    function grade_modify(){
        $data=I('post.');
        $storeArray = $this->storeArray;
        $store_id = $storeArray['id'];
        $memberModel=M('member');
        $info['id']=$data['id'];
        $rs1=$memberModel->find($data['id']);
        if(empty($rs1)){
            $this->ajax_r(0,"该会员不存在");
        }
        $info['grade']=$data['grade'];
        $rs=$memberModel->save($info);
        if($rs){
            $log['uid']=$rs1['user_id'];
            $log['member_id']=$rs1['id'];
            $log['addtime']=time();
            $log['num']=abs($data['grade']);
            $log['pre_num']=abs($rs1['grade']);
            if($data['grade']<0){
                $log['type']=0;
            }else{
                $log['type']=1;
            }
            $log['store_id']=$store_id;
            $log['remark']='店铺后台等级修改';
            $log['num_type']=2;
            M('member_log')->add($log);
            $this->ajax_r(1,"等级变更成功",array('url' => U('Member/index')));
        }else{
            $this->ajax_r(0,"等级变更失败");
        }
    }
    function update_grade(){
        $data=I('post.');
        $storeArray = $this->storeArray;
        $store_id = $storeArray['id'];
        $member_grade=M('member_grade');
        $info['id']=$data['id'];
        $info['gradeName']=$data['gradeName'];
        $info['money']=$data['money'];
        $info['min_integral']=$data['min_integral'];
        $info['discount']=$data['discount'];
        $info['points']=$data['points'];
        $info['daiyan']=$data['daiyan'];
        $info['store_id']=$store_id;
        $info['status']=1;
        $info['create_time']=time();
        //print_r($info);
        $rs=$member_grade->save($info);
        //print_r($member_grade->getLastSql());
        if($rs){
            $this->ajax_r(1,"修改等级成功",array('url' => U('Member/grade')));
        }else{
            $this->ajax_r(0,"修改等级失败",array('url' => U('Member/addgrade')));
        }
    }
    function grade_del(){
        if(isset($_POST['id'])){
            $member_grade=M('member_grade');
            $id = intval(I("post.id",0));
            $grade_id=$member_grade->where('id='.$id)->delete();
            //print_r($member_grade->getLastSql());
            if ($grade_id) {
                $this->ajax_r ( 1,'删除成功 ',array('url' => U('Member/grade')));
            } else {
                $this->ajax_r ( 0,'删除失败 ');
            }
        }
    }

    function integraldetail(){
        $id=I('get.id');
        $users_log=M('users_log');
        $users=M('users');
        $user_info=$users->find($id);
        $count= $users_log->where('uid='.$id)->count();
        $this->assign('count',$count);

        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show       = $Page->show();// 分页显示输出
        $users_list=$users_log
            ->where('uid='.$id)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('users_list',$users_list);
        $this->assign('user_info',$user_info);
        $this->assign('title','会员积分列表');
        $this->display();

    }

    public function expense_info()
    {
        $id = I('get.id');
        $storeArray = $this->storeArray;
        $store_id = $storeArray['id'];

        $member_info = M('member')->find($id);
        $this->assign('member_info', $member_info);
        $count = M('order')
            ->join('yt_member on yt_member.id=yt_order.member_id')
            ->where("yt_order.store_id=$store_id and yt_order.order_state>10 and yt_order.refund_state=0")
            ->count();

        $Page = new \Think\Page ($count, 15); // 实例化分页类 传入总记录数
        $Page->setConfig('header', '条记录');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $show = $Page->show(); // 分页显示输出
        $rs =M('order')
            ->join('yt_member on yt_member.id=yt_order.member_id')
            ->where("yt_order.store_id=$store_id and yt_order.order_state>10 and yt_order.refund_state=0")
            ->group('yt_order.member_id')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('yt_order_goods.rec_id desc')
            ->select();
        $this->assign('page', $show);
        $this->assign('rs', $rs);
        $this->display();
    }

    public function search_phone(){
        $phone=I('get.phone');
        $users=M('users');
        $rs=$users->where('phone='.$phone)->find();
        if($rs){
            $this->ajaxReturn($rs);
        }else{
            $this->ajaxReturn(0);
        }
    }

}
